package com.example.Android_test.activity.training;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import com.example.Android_test.R;
import com.example.Android_test.activity.Main;
import com.example.Android_test.helpers.SOWEngine;
import com.example.Android_test.helpers.SettingsHelper;
import com.example.Android_test.specs.WordSpec;

import java.util.ArrayList;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: anpi0413
 * Date: 21.05.13
 * Time: 11:33
 * To change this template use File | Settings | File Templates.
 */
public class Training_Guess_Description extends Abstract_Training {
    private TextView gd_textViewWordDescription;
    private TextView gd_textViewDescriptionTranslation;

    private Button var1Button;
    private Button var2Button;
    private Button var3Button;
    private Button var4Button;

    private Button gd_buttonNext;

    private WordSpec trainingWord = null;

    @Override
    protected void onFirstRun() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    protected void next() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    protected String getKeyTrainingTimeSpent() {
        return SettingsHelper.KEY_TRAINING_TIME_SPENT_GD;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.training_guess_description);

        gd_textViewWordDescription = (TextView) findViewById(R.id.gd_textViewWordDescription);
        gd_textViewDescriptionTranslation = (TextView) findViewById(R.id.gd_textViewDescriptionTranslation);

        var1Button = (Button) findViewById(R.id.gd_buttonVar1);
        var2Button = (Button) findViewById(R.id.gd_buttonVar2);
        var3Button = (Button) findViewById(R.id.gd_buttonVar3);
        var4Button = (Button) findViewById(R.id.gd_buttonVar4);

        gd_buttonNext = (Button)  findViewById(R.id.gd_buttonNext);
    }

    @Override
    protected void trainingStart() {
        super.trainingStart();

        prepareVariantButtons();

        try {
            trainingWord = SOWEngine.INSTANCE.getWordToTraining();
        } catch (Exception e) {
            Log.e(Main.LOG_TAG, e.getMessage());
            return;
        }

        gd_textViewWordDescription.setText(prepareDescription(trainingWord));
        gd_textViewDescriptionTranslation.setText(prepareDescription(trainingWord));


        ArrayList<WordSpec> randWords;
        try {
            randWords = SOWEngine.INSTANCE.getRandomWords(trainingWord, 3);
        } catch (Exception e) {
            Log.e(Main.LOG_TAG, e.getMessage());
            return;
        }

        randWords.add(trainingWord);

        /*newWordTextView.setText(trainingWord.getWord());

        rightRusWord = trainingWord.getTranslationVariantSpecs().get(0).getTranslate();
*/
        Random rand = new Random();
        for (int i=0; i < 4; i++) {
            int randNum = rand.nextInt(randWords.size());

            switch (i) {
                case 0: var1Button.setText(randWords.get(randNum).getTranslationVariantSpecs().get(0).getTranslate());
                    break;
                case 1: var2Button.setText(randWords.get(randNum).getTranslationVariantSpecs().get(0).getTranslate());
                    break;
                case 2: var3Button.setText(randWords.get(randNum).getTranslationVariantSpecs().get(0).getTranslate());
                    break;
                case 3: var4Button.setText(randWords.get(randNum).getTranslationVariantSpecs().get(0).getTranslate());
                    break;
                default: break;
            }

            randWords.remove(randNum);
        }
    }

    private String prepareDescription(WordSpec word) {
        return "* * * " + word.getDescription().substring(word.getDescription().indexOf("-"));
    }


    private void enableAllVariantButtons() {
        var1Button.setEnabled(true);
        var2Button.setEnabled(true);
        var3Button.setEnabled(true);
        var4Button.setEnabled(true);
    }

    private void disableAllVariantButtons() {
        var1Button.setEnabled(false);
        var2Button.setEnabled(false);
        var3Button.setEnabled(false);
        var4Button.setEnabled(false);
    }

    private void prepareVariantButtons() {
        var1Button.setBackgroundResource(R.drawable.button_custom);
        var2Button.setBackgroundResource(R.drawable.button_custom);
        var3Button.setBackgroundResource(R.drawable.button_custom);
        var4Button.setBackgroundResource(R.drawable.button_custom);

        enableAllVariantButtons();
    }


    @Override
    protected void trainingStop() {
        super.trainingStop();
        //To change body of implemented methods use File | Settings | File Templates.
    }
}